package blog.controller.articles;

import blog.utils.QiniuUtils;
import blog.vo.Result;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

@RestController
@RequestMapping("/upload")
public class UploadController {

    @PostMapping
    public Result upload(@RequestParam("image") MultipartFile file) throws IOException {

        java.lang.String originalFilename = file.getOriginalFilename();
        String fileName=IdUtil.simpleUUID()+'.'+StrUtil.subAfter(originalFilename, '.', true);

        boolean flag = QiniuUtils.upload(file, fileName);

        if (flag){
            return Result.success(QiniuUtils.url+fileName);
        }

        return Result.fail(20001,"上传失败！");
    }
}